﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using log4net;
using System.Reflection;

namespace Cn.Edwon.Data {
    public class ADO4Access : AccessSupport, ICommonProcess {
        public static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public ADO4Access(String connString) {
            base.ConnectString = connString;
        }
        public ADO4Access(String dataSource, String password) {
            if (String.IsNullOrEmpty(password)) {
                base.ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;OLE DB Services=-2;Data Source="
                                + dataSource ;
            } else {
                base.ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;OLE DB Services=-2;Data Source="
                                + dataSource + ";Jet OLEDB:Database Password=" + password;
            }
        }
        
        #region ICommonProcess 成员

        public int CommonUpdateAction(string tsql) {
            this.Sql = tsql;
            log.Debug("tsql:[" + tsql + "]");
            using (OleDbConnection conn = new OleDbConnection(ConnectString)) {
                OleDbCommand odc = new OleDbCommand(tsql, conn);
                conn.Open();
                return odc.ExecuteNonQuery();
            }
        }

        public DataSet CommonSelectAction(string tsql, string tableName) {
            this.Sql = tsql;
            log.Debug("tsql:[" + tsql + "]");
            using (OleDbConnection conn = new OleDbConnection(ConnectString)) {
                OleDbCommand odc = new OleDbCommand(tsql, conn);
                odc.CommandType = CommandType.Text;
                conn.Open();
                using (OleDbDataAdapter da = new OleDbDataAdapter(odc)) {
                    DataSet ds = new DataSet();
                    da.Fill(ds, tableName);
                    return ds;
                }
            }
        }

        #endregion
    }
}
